/* 

编写一个程序，找到两个单链表相交的起始节点。

*/

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 */

var getIntersectionNode = function (headA, headB) {
    let n1 = headA;
    let n2 = headB;

    while (n1 !== n2) {
        if (n1 === null) {
            n1 = headB;
        } else {
            n1 = n1.next;
        }

        if (n2 === null) {
            n2 = headA;
        } else {
            n2 = n2.next;
        }
    }

    return n1;
};